package com.heather.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heather.exam.pojo.table.Class_info;
import com.heather.exam.pojo.table.User;
import com.heather.exam.pojo.vo.UserInClassVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ClassMapper extends BaseMapper<Class_info> {


    Integer addClass(String name, Integer userId);
    Integer joinClass(String classId,Integer userId);
    boolean existsByClassIdAndUserId(String classId, int userId);

    // 可以在插入前调用此方法进行查重
    default boolean checkAndJoinClass(String classId, int userId) {
        return !existsByClassIdAndUserId(classId, userId);
    }

    @Select("SELECT c.user_id AS userId, u.username FROM class c INNER JOIN user u ON c.user_id = u.id WHERE c.class_id = #{classId}")
    List<UserInClassVO> getAllClass(String classId);

    @Select("SELECT c.user_id AS userId, u.username FROM applyclass c INNER JOIN user u ON c.user_id = u.id WHERE c.class_id = #{classId}")
    List<UserInClassVO> joinList(String classId);

    @Select("SELECT score from score where user_id=#{userid} and exam_id = #{examid}")
    int getScore(String userid,String examid);
}
